/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.phone.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.phone.dao.ManufacturerDao;
import com.jeesite.modules.phone.dao.PhoneCommodityDao;
import com.jeesite.modules.phone.dao.StaffDao;
import com.jeesite.modules.phone.entity.Manufacturer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.phone.entity.PhoneCommodity;
import com.jeesite.modules.phone.service.PhoneCommodityService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商品Controller
 * @author lzw
 * @version 2019-04-03
 */
@Controller
@RequestMapping(value = "${adminPath}/phone/phoneCommodity")
public class PhoneCommodityController extends BaseController {

	@Autowired
	private ManufacturerDao manufacturerDao;

	@Autowired
	private PhoneCommodityService phoneCommodityService;

	@Autowired
	private PhoneCommodityDao phoneCommodityDao;

	@Autowired
	private StaffDao staffDao;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public PhoneCommodity get(Long id, boolean isNewRecord) {
		return phoneCommodityService.get(id+"", isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"list", ""})
	public String list(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/phoneCommodityList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<PhoneCommodity> listData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = phoneCommodityService.findPage(new Page<PhoneCommodity>(request, response), phoneCommodity);
		return page;
	}

	/**
	 * 查询日记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"daylist", ""})
	public String daylist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/dayRecordList";
	}

	/**
	 * 查询日记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "daylistData")
	@ResponseBody
	public Page<PhoneCommodity> daylistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		page.setList(phoneCommodityDao.obtainDayInfo());
		page.setPageSize(30);
		return page;
	}



	/**
	 * 查询厂商日记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"manufacturerDaylist", ""})
	public String manufacturerDaylist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/manufacturerDayRecordList";
	}

	/**
	 * 查询厂商日记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "manufacturerDaylistData")
	@ResponseBody
	public Page<PhoneCommodity> manufacturerDaylistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		page.setList(phoneCommodityDao.obtainManufacturerDayInfo());
		page.setPageSize(30);
		return page;
	}


	/**
	 * 查询商品日记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"commodityDaylist", ""})
	public String commodityDaylist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/commodityDayRecordList";
	}

	/**
	 * 查询商品日记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "commodityDaylistData")
	@ResponseBody
	public Page<PhoneCommodity> commodityDaylistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		page.setList(phoneCommodityDao.obtainCommodityDayInfo(phoneCommodity.getProductManufacturerId()));
		page.setPageSize(30);
		return page;
	}





	/**
	 * 查询月记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"monthlist", ""})
	public String monthlist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/monthRecordList";
	}

	/**
	 * 查询月记录列表数据obtainQuarterInfo
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "monthlistData")
	@ResponseBody
	public Page<PhoneCommodity> monthlistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		if(phoneCommodity!=null&&phoneCommodity.getYrars()!=""&&phoneCommodity.getYrars()!=null){
			page.setList(phoneCommodityDao.obtainMonthInfo(phoneCommodity.getYrars()));
		}else{
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			page.setList(phoneCommodityDao.obtainMonthInfo(sdf.format(date)));
		}
		page.setPageSize(30);
		return page;
	}

	/**
	 * 查询厂商月记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"manufacturerMonthlist", ""})
	public String manufacturerMonthlist(PhoneCommodity phoneCommodity, Model model) {
		phoneCommodity.setMonths(phoneCommodity.getYrars().substring(5,7));
		phoneCommodity.setYrars(phoneCommodity.getYrars().substring(0,4));
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/manufacturermonthRecordList";
	}

	/**
	 * 查询厂商月记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "manufacturerMonthlistData")
	@ResponseBody
	public Page<PhoneCommodity> manufacturerMonthlistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		page.setList(phoneCommodityDao.obtainManufacturerMonthInfo(phoneCommodity.getYrars(),phoneCommodity.getMonths()));
		page.setPageSize(30);
		return page;
	}

	/**
	 * 查询商品月记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"commodityMonthlist", ""})
	public String commodityMonthlist(PhoneCommodity phoneCommodity, Model model) {
		phoneCommodity.setMonths(phoneCommodity.getYrars().substring(5,7));
		phoneCommodity.setYrars(phoneCommodity.getYrars().substring(0,4));
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/commoditymonthRecordList";
	}

	/**
	 * 查询商品月记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "commodityMonthlistData")
	@ResponseBody
	public Page<PhoneCommodity> commodityMonthlistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		page.setList(phoneCommodityDao.obtainCommodityMonthInfo(phoneCommodity.getYrars(),phoneCommodity.getMonths(),phoneCommodity.getProductManufacturerId()));
		page.setPageSize(30);
		return page;
	}


	/**
	 * 查询季度记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"quarterlist", ""})
	public String quarterlist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/quarterRecordList";
	}

	/**
	 * 查询季度记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "quarterlistData")
	@ResponseBody
	public Page<PhoneCommodity> quarterlistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		if(phoneCommodity!=null&&phoneCommodity.getYrars()!=""&&phoneCommodity.getYrars()!=null){
			page.setList(phoneCommodityDao.obtainQuarterInfo(phoneCommodity.getYrars()));
		}else{
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			page.setList(phoneCommodityDao.obtainQuarterInfo(sdf.format(date)));
		}
		page.setPageSize(30);
		return page;
	}



	/**
	 * 查询厂家季度记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"manufacturerQuarterlist", ""})
	public String manufacturerQuarterlist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/manufactureQuarterRecordList";
	}

	/**
	 * 查询厂家季度记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "manufacturerQuarterlistData")
	@ResponseBody
	public Page<PhoneCommodity> manufacturerQuarterlistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		page.setList(phoneCommodityDao.obtainManufacturerQuarterInfo(phoneCommodity.getYrars(),phoneCommodity.getQuarters()));
		page.setPageSize(30);
		return page;
	}

	/**
	 * 查询商品季度记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"commodityQuarterlist", ""})
	public String commodityQuarterlist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/commodityQuarterRecordList";
	}

	/**
	 * 查询商品季度记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "commodityQuarterlistData")
	@ResponseBody
	public Page<PhoneCommodity> commodityQuarterlistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		page.setList(phoneCommodityDao.obtainCommodityQuarterInfo(phoneCommodity.getYrars(),phoneCommodity.getQuarters(),phoneCommodity.getProductManufacturerId()));
		page.setPageSize(30);
		return page;
	}



	/**
	 * 查询年记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"yearlist", ""})
	public String yearlist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/yearRecordList";
	}

	/**
	 * 查询年记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "yearlistData")
	@ResponseBody
	public Page<PhoneCommodity> yearlistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		if(phoneCommodity!=null&&phoneCommodity.getYrars()!=""&&phoneCommodity.getYrars()!=null){
			page.setList(phoneCommodityDao.obtainYearInfo(phoneCommodity.getYrars()));
		}else{
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			page.setList(phoneCommodityDao.obtainYearInfo(sdf.format(date)));
		}
		page.setPageSize(30);
		return page;
	}



	/**
	 * 查询厂商年记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"manufacturerYearlist", ""})
	public String manufacturerYearlist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/manufacturerYearRecordList";
	}

	/**
	 * 查询厂商年记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "manufacturerYearlistData")
	@ResponseBody
	public Page<PhoneCommodity> manufacturerYearlistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		page.setList(phoneCommodityDao.obtainManufacturerYearInfo(phoneCommodity.getYrars()));
		page.setPageSize(30);
		return page;
	}


	/**
	 * 查询商品年记录列表
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = {"commodityYearlist", ""})
	public String commodityYearlist(PhoneCommodity phoneCommodity, Model model) {
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/commodityYearRecordList";
	}

	/**
	 * 查询商品年记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "commodityYearlistData")
	@ResponseBody
	public Page<PhoneCommodity> commodityYearlistData(PhoneCommodity phoneCommodity, HttpServletRequest request, HttpServletResponse response) {
		Page<PhoneCommodity> page = new Page<PhoneCommodity>();
		page.setList(phoneCommodityDao.obtainCommodityYearInfo(phoneCommodity.getYrars(),phoneCommodity.getProductManufacturerId()));
		page.setPageSize(30);
		return page;
	}


	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "form")
	public String form(PhoneCommodity phoneCommodity, Model model) {
		phoneCommodity.setManufacturerList(manufacturerDao.obtainManufacturer());
		phoneCommodity.setStaffList(staffDao.obtainStaff());
		model.addAttribute("phoneCommodity", phoneCommodity);
		return "modules/phone/phoneCommodityForm";
	}

	/**
	 * 保存商品
	 */
	@RequiresPermissions("phone:phoneCommodity:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated PhoneCommodity phoneCommodity) {
		phoneCommodity.setTotalPrice(phoneCommodity.getPrice()*phoneCommodity.getNumber());
		phoneCommodityService.save(phoneCommodity);
		return renderResult(Global.TRUE, "保存商品成功！");
	}
	
	/**
	 * 停用商品
	 */
	@RequiresPermissions("phone:phoneCommodity:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(PhoneCommodity phoneCommodity) {
		phoneCommodity.setStatus(PhoneCommodity.STATUS_DISABLE);
		phoneCommodityService.updateStatus(phoneCommodity);
		return renderResult(Global.TRUE, "停用商品成功");
	}
	
	/**
	 * 启用商品
	 */
	@RequiresPermissions("phone:phoneCommodity:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(PhoneCommodity phoneCommodity) {
		phoneCommodity.setStatus(PhoneCommodity.STATUS_NORMAL);
		phoneCommodityService.updateStatus(phoneCommodity);
		return renderResult(Global.TRUE, "启用商品成功");
	}
	
	/**
	 * 删除商品
	 */
	@RequiresPermissions("phone:phoneCommodity:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(PhoneCommodity phoneCommodity) {
		phoneCommodityService.delete(phoneCommodity);
		return renderResult(Global.TRUE, "删除商品成功！");
	}
	
}